package com.libs.sys.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.libs.sys.mapper.UserseatMapper;
import com.libs.sys.model.Userseat;
import com.libs.sys.service.UserseatService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * InnoDB free: 10240 kB 服务实现类
 * </p>
 *
 * @author SR.Y
 * @since 2021-08-11
 */
@Service
public class UserseatServiceImpl extends ServiceImpl<UserseatMapper, Userseat> implements UserseatService {

    @Override
    public boolean add(Userseat userseat) {
        return save(userseat);
    }

    @Override
    public boolean update(Userseat userseat) {
        return updateById(userseat);
    }

    @Override
    public boolean delete(List<Integer> idList) {
        return removeByIds(idList);
    }

    @Override
    public Userseat getOneById(Integer id) {
        return getById(id);
    }

    @Override
    public Page<Userseat> list(Integer pageNum, Integer pageSize) {
        Page<Userseat> page = new Page<>(pageNum, pageSize);
        return page(page);
    }

    @Override
    public Page<Userseat> list(Userseat userseat, Integer pageNum, Integer pageSize) {
        final String uid = userseat.getUid();
        final String seatid = userseat.getSeatid();
        Page<Userseat> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Userseat> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<Userseat> lambda = wrapper.lambda();
        if(StrUtil.isNotEmpty(uid)&&StrUtil.isNotEmpty(seatid)){
            lambda.eq(Userseat::getUid,uid);
        }
        return page(page,wrapper);
    }

    @Override
    public boolean isHave(Userseat userseat) {
        Userseat u = new Userseat();
        u.setUid(userseat.getUid());
        return list(u, 0, 10).getTotal() > 0;
    }

    @Override
    public boolean isHaveSeat(Userseat userseat) {
        final String uid = userseat.getUid();
        final String seatid = userseat.getSeatid();
        Page<Userseat> page = new Page<>(0, 10);
        QueryWrapper<Userseat> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<Userseat> lambda = wrapper.lambda();
        if(StrUtil.isNotEmpty(seatid)){
            lambda.eq(Userseat::getSeatid,seatid);
        }
        return page(page,wrapper).getTotal()>0;
    }

    @Override
    public Userseat login(String uid,String pwd) {
        Page<Userseat> page = new Page<>(0, 10);
        QueryWrapper<Userseat> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<Userseat> lambda = wrapper.lambda();
        if(StrUtil.isNotEmpty(uid)){
            lambda.eq(Userseat::getUid,uid);
        }if(StrUtil.isNotEmpty(pwd)){
            lambda.eq(Userseat::getPwd,pwd);
        }
        final Page<Userseat> page1 = page(page, wrapper);
        if( page1.getTotal()>0){
           return page1.getRecords().get(0);
       }else {
            return null;
        }
    }

}
